package com.dhcc.core.modules.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.gen.dao.TableMapper;
import com.dhcc.core.modules.gen.entity.Column;
import com.dhcc.core.modules.gen.entity.Table;
import com.dhcc.core.modules.system.dao.SplitTablesMapper;
import com.dhcc.core.modules.system.entity.SplitTablesEntity;
import com.dhcc.core.modules.system.service.ISplitTablesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统分表对象表表数据服务层接口
 * 
 * @ClassName: SplitTablesServiceImpl
 * @Description: TODO
 * @author syj
 * @date 2022-03-14 14:04:56
 */
@Service("splitTablesService")
public class SplitTablesServiceImpl extends BaseServiceImpl<SplitTablesMapper, SplitTablesEntity> implements ISplitTablesService {

	@Autowired
	private TableMapper tableMapper;

	@Override
	public List<SplitTablesEntity> page(Page<SplitTablesEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<SplitTablesEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public List<Map<String, Object>> selectTables(String condition) {
		return this.baseMapper.selectTables(condition);
	}
	@Override
	public List<ZTreeNode> tree() {
		List<ZTreeNode> tree = this.baseMapper.tree();
		return tree;
	}
	@Override
	@Transactional(readOnly = false)
	public void delTableById(Long tableId) {
		Wrapper<SplitTablesEntity> d_wrapper = new EntityWrapper<>();
		d_wrapper = d_wrapper.eq("parent_id", tableId);
		Integer d_ls = this.baseMapper.selectCount(d_wrapper);
		if (d_ls == null || d_ls > 0) {
			throw new BizException("该表下有子表存在!");
		}
		// 删除表
		this.baseMapper.deleteById(tableId);
	}
	/**
	 * 通过角色id获取角色名称
	 */
	@Override
	public String getTableCodeById(Long tableId) {
		if (CommonUtil.isEmpty(tableId)||0 == tableId) {
			return null;
		}
		SplitTablesEntity tableObj = this.baseMapper.selectById(tableId);
		if (CommonUtil.isNotEmpty(tableObj)) {
			return tableObj.getTableName();
		}
		return "";
	}

	@Override
	public JSONObject queryTableMap(Map<String, Object> map) {
		JSONObject jsObj = new JSONObject(new LinkedHashMap());
		List<Table> tables = tableMapper.queryList(map);
		jsObj.put("","请选择");
		for (Table table : tables) {
			jsObj.put(table.getTableName(),table.getTableName());
		}
		return jsObj;
	}

	@Override
	public JSONArray queryColumn(String tableName) {
		JSONArray jsonArray = new JSONArray();
		List<Column> columns = tableMapper.queryColumns(tableName);
		for (Column column : columns) {
			JSONObject jsObj = new JSONObject(new LinkedHashMap());
			jsObj.put("value",column.getColumnName());
			jsObj.put("text",column.getColumnName());
			jsonArray.add(jsObj);
		}
		return jsonArray;
	}
}